package top.hymsk.ecommerce.controller.restful.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.hymsk.ecommerce.anno.Log;
import top.hymsk.ecommerce.anno.TokenAnalysis;
import top.hymsk.ecommerce.pojo.DTO.PlaceProductDTO;
import top.hymsk.ecommerce.pojo.response.GeneralResponse;
import top.hymsk.ecommerce.service.OrderService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 买家订单 /order/buyer
 *
 * @author 花于陌上开
 */
@CrossOrigin  //允许跨域访问
@RestController
@RequestMapping("/order/buyer")
public class BuyerOrderRestController {
    private final HttpServletRequest request;
    private final OrderService orderService;

    BuyerOrderRestController(HttpServletRequest request, OrderService orderService) {
        this.request = request;
        this.orderService = orderService;
    }

    /**
     * 获取订单列表
     * (基本完成)
     *
     * @param token 用户鉴权token
     * @param page  分页参数
     * @return 响应信息
     */
    @Log
    @TokenAnalysis
    @PostMapping("/list")
    public GeneralResponse getOrderList(String token, Integer page) {
        Integer userId = (Integer) request.getAttribute("userId");
        return orderService.getOrderListByBuyer(userId, page);
    }

    /**
     * 下单
     * (基本完成)
     *
     * @param token   用户鉴权token
     * @param details 详细信息 [{"productId":1,"number":3},{"productId":3,"number":1},{"productId":5,"number":1}]
     * @return 响应信息
     */
    @Log
    @TokenAnalysis
    @PostMapping("/place")
    public GeneralResponse place(String token, String details) {
        Integer userId = (Integer) request.getAttribute("userId");
        //解析详细信息
        List<PlaceProductDTO> placeProductList = JSON.parseObject(details, new TypeReference<List<PlaceProductDTO>>() {});
        return orderService.place(userId, placeProductList);
    }

    /**
     * 取消订单
     *
     * @param token   用户鉴权token
     * @param orderId 订单id
     * @return 响应信息
     */
    @Log
    @TokenAnalysis
    @PostMapping("/cancel")
    public GeneralResponse cancel(String token, Integer orderId) {
        Integer userId = (Integer) request.getAttribute("userId");
        return orderService.cancel(userId,orderId);
    }

    /**
     * 订单商品退款
     *
     * @param token   用户鉴权token
     * @param orderId 订单id
     * @param productId 商品id
     * @return 响应信息
     */
    @Log
    @TokenAnalysis
    @PostMapping("/refund")
    public GeneralResponse refund(String token, Integer orderId,Integer productId) {
        Integer userId = (Integer) request.getAttribute("userId");
        return orderService.refund(userId,orderId,productId);
    }

    /**
     * 支付
     *
     * @param token   用户鉴权token
     * @param orderId 订单id
     * @return 响应信息
     */
    @Log
    @TokenAnalysis
    @PostMapping("/payment")
    public GeneralResponse payment(String token, Integer orderId) {
        Integer userId = (Integer) request.getAttribute("userId");
        return orderService.payment(userId, orderId);
    }

    /**
     * 查看订单状态
     *
     * @param token   用户鉴权token
     * @param orderId 订单id
     * @return 响应信息
     */
    @Log
    @TokenAnalysis
    @PostMapping("/status")
    public GeneralResponse status(String token, Integer orderId) {
        Integer userId = (Integer) request.getAttribute("userId");
        return orderService.getStatusByBuyer(userId, orderId);
    }

    /**
     * 收货
     *
     * @param token   用户鉴权token
     * @param orderId 订单id
     * @return 响应信息
     */
    @Log
    @TokenAnalysis
    @PostMapping("/receive")
    public GeneralResponse receive(String token, Integer orderId) {
        Integer userId = (Integer) request.getAttribute("userId");
        return orderService.receive(userId, orderId);
    }

//    /**
//     * 获取商品待评论列表
//     *
//     * @param token   用户鉴权token
//     * @param orderId 订单id
//     * @return 响应信息
//     */
//    @Log
//    @TokenAnalysis
//    @PostMapping("/remark/waitList")
//    public GeneralResponse getRemarkWaitList(String token, Integer orderId) {
//        Integer userId = (Integer) request.getAttribute("userId");
//        return orderService.getRemarkWaitList(userId, orderId);
//    }

    /**
     * 商品评论
     *
     * @param token     用户鉴权token
     * @param orderId   订单id
     * @param productId 商品id
     * @param remark    评论
     * @param star      评分
     * @return 响应信息
     */
    @Log
    @TokenAnalysis
    @PostMapping("/remark")
    public GeneralResponse remark(String token, Integer orderId, Integer productId, String remark, Double star) {
        Integer userId = (Integer) request.getAttribute("userId");
        //进行类型检查
        return orderService.remark(userId, orderId, productId, remark, star);
    }

    /*发起退款*/

}
